/******************************************************************
 * Copyright (c) 2007 - 2007, Simon Guerard, All rights reserved.
 * 
 * Projet de jeu de course au large.
 * 
 * This File is part of Sail-Race
 * 
 * Sail-Race is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 * 
 * Sail-Race is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Sail-Race; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
package net.simcafamily.sailrace.panel;

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import com.jgoodies.common.base.SystemUtils;

/**
 * Ce panneau represente l'en-tete d'un formulaire. Un en-tete affiche un logo
 * (dont l'opacite peut etre modifiee) et un titre.
 * 
 * @author Roguy
 */
public class HeaderPanel extends JPanel {

	/**
     * 
     */
	private static final long serialVersionUID = 6304580072385895817L;
	private final Color blender;
	private final ImageIcon icon;
	private static AlphaComposite composite = AlphaComposite
			.getInstance(AlphaComposite.SRC_OVER);

	/**
	 * Cree un nouvel en-tete affichant l'image selectionnee, avec l'opacite
	 * choisie et le titre specifie.
	 * 
	 * @param title
	 *            Le titre de l'en-tete
	 * @param icon
	 *            L'image d'en-tete
	 * @param alpha
	 *            L'opacite de l'image
	 */
	public HeaderPanel(final String title, final ImageIcon icon,
			final float alpha) {
		super(new BorderLayout());

		this.icon = icon;
		blender = new Color(255, 255, 255, (int) (255 * alpha));

		final JLabel headerTitle = new JLabel(title);
		final Font font = headerTitle.getFont().deriveFont(Font.BOLD, 25.0f);
		headerTitle.setFont(font);
		headerTitle.setBorder(new EmptyBorder(0, 0, 0, 4));
		headerTitle.setForeground(getHeaderBackground());
		add(BorderLayout.EAST, headerTitle);

		if (icon != null) {
			setPreferredSize(new Dimension(this.icon.getIconWidth(),
					this.icon.getIconHeight()));
		}
	}

	/**
	 * Recupere la couleur de l'en-tete en fonction du theme choisi.
	 * 
	 * @return Le couleur de fond de l'en-tete
	 */
	protected Color getHeaderBackground() {
		Color c = UIManager
				.getColor("SimpleInternalFrame.activeTitleBackground");
		if (c != null) {
			return c;
		}
		if (SystemUtils.IS_LAF_WINDOWS_XP_ENABLED) {
			c = UIManager.getColor("InternalFrame.activeTitleGradient");
		}
		return c != null ? c : UIManager
				.getColor("InternalFrame.activeTitleBackground");
	}

	/**
	 * Dessine un degrade dans le composant.
	 * 
	 * @param g
	 *            L'objet graphique sur lequel peindre
	 */
	@Override
	public void paintComponent(final Graphics g) {
		super.paintComponent(g);
		if (!isOpaque()) {
			return;
		}

		final Color control = UIManager.getColor("control");
		final int width = getWidth();
		final int height = getHeight();

		final Graphics2D g2 = (Graphics2D) g;

		g2.setRenderingHint(RenderingHints.KEY_RENDERING,
				RenderingHints.VALUE_RENDER_QUALITY);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
				RenderingHints.VALUE_FRACTIONALMETRICS_ON);

		final Paint storedPaint = g2.getPaint();
		g2.setPaint(new GradientPaint(icon.getIconWidth(), 0, Color.white,
				width, 0, control));
		g2.fillRect(0, 0, width, height);
		g2.setPaint(storedPaint);

		g2.drawImage(icon.getImage(), 0, 0, this);
		g2.setColor(blender);
		g2.setComposite(HeaderPanel.composite);
		g2.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
	}
}
